package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.model.TDriver;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.service.ITDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 司机管理控制器
 *
 * @author fengshuonan
 * @Date 2018-01-30 15:51:51
 */
@Controller
@RequestMapping("/tDriver")
public class TDriverController extends BaseController {

    private String PREFIX = "/system/tDriver/";

    @Autowired
    private ITDriverService tDriverService;

    /**
     * 跳转到司机管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tDriver.html";
    }

    /**
     * 跳转到添加司机管理
     */
    @RequestMapping("/tDriver_add")
    public String tDriverAdd() {
        return PREFIX + "tDriver_add.html";
    }

    /**
     * 跳转到修改司机管理
     */
    @RequestMapping("/tDriver_update/{tDriverId}")
    public String tDriverUpdate(@PathVariable Integer tDriverId, Model model) {
        TDriver tDriver = tDriverService.selectById(tDriverId);
        model.addAttribute("item",tDriver);
        LogObjectHolder.me().set(tDriver);
        return PREFIX + "tDriver_edit.html";
    }

    /**
     * 获取司机管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) Integer deptid,Integer limit, Integer offset) {
        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",deptid==null? ShiroKit.getUser().getDeptId():deptid);
        m.put("name",name);
        DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
        List<Map<String, Object>> driverList ;
        if (ShiroKit.isAdmin()) {
           driverList = tDriverService.selectDriverList(page,null,name,deptid);
        }else{
            driverList = tDriverService.selectDriverList(page,dataScope,name,deptid);
        }

        pageInfoBT.setRows(driverList);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }
    /**
     * 新增司机管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TDriver tDriver) {
        tDriver.setCreateUser(ShiroKit.getUser().getId());
        tDriver.setDeptId(ShiroKit.getUser().getDeptId());
        TDriver driver = tDriverService.getByDriver(tDriver.getDriverName(),tDriver.getId());
        if (driver != null) {
            throw new GunsException(BizExceptionEnum.DRIVER_ALREADY_REG);
        }
        tDriverService.insert(tDriver);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除司机管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tDriverId) {
        tDriverService.deleteById(tDriverId);
        return SUCCESS_TIP;
    }

    /**
     * 修改司机管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TDriver tDriver) {
        TDriver driver = tDriverService.getByDriver(tDriver.getDriverName(),tDriver.getId());
        if (driver != null) {
            throw new GunsException(BizExceptionEnum.DRIVER_ALREADY_REG);
        }
        tDriverService.updateById(tDriver);
        return super.SUCCESS_TIP;
    }

    /**
     * 司机管理详情
     */
    @RequestMapping(value = "/detail/{tDriverId}")
    @ResponseBody
    public Object detail(@PathVariable("tDriverId") Integer tDriverId) {
        return tDriverService.selectById(tDriverId);
    }
}
